package com.cci.kangdao.service.impl;

import com.cci.kangdao.common.RedisKeys;
import com.cci.kangdao.dao.RelationShipTDao;
import com.cci.kangdao.dao.WorkOrderRepairDao;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.RelationShipT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dao.model.WorkOrderT;
import com.cci.kangdao.redis.RedisClientUtils;
import com.cci.kangdao.service.PushNotifyService;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.NotifyUtil;
import com.cci.kangdao.utilTool.SmsUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2023/10/8 10:23
 * @version: V1.0
 **/
@Service
public class PushNotifyServiceImpl implements PushNotifyService {

    private Logger log = Logger.getLogger(this.getClass().getName());
    @Resource
    private WorkOrderRepairDao workOrderRepairDao;
    @Resource
    private WorkOrderTDao workOrderTDao;
    @Autowired
    private RedisClientUtils redisClientUtils;
    @Resource
    private RelationShipTDao relationShipTDao;

//    /**
//     * orderid 工单id   发送的消息
//     *
//     * @param orderId
//     * @param message
//     */
//    @Override
//    public void pushNotify(Long orderId, String message) {
//        //
//        WorkOrderT byOrderId = workOrderTDao.getWorkOrderTByOrderId(orderId);
//        if (byOrderId != null) {
//            this.pushNotify(byOrderId, message);
//        } else {
//            log.error("pushNotify:（orderId）-------未获取到 工单信息");
//        }
//    }
//
//    /**
//     * workOrderT 工单   发送的消息
//     *
//     * @param workOrderT
//     * @param message
//     */
//    @Override
//    public void pushNotify(WorkOrderT workOrderT, String message) {
////        workOrderT.get
//    }

    /**
     * userList 发送人消息   发送的消息
     *
     * @param userList
     * @param message
     */
    @Override
    public void pushNotify(List<UserT> userList, String message, Long workOrderId, String title) {
        if (CollectionUtils.isNotEmpty(userList)) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (UserT user : userList) {
                //发送短信
                Map<String, Object> userNotifyRecordTMap = this.getNotifyRecordParam(message, workOrderId, 1, user);
                String sendResult = SmsUtils.sendMessageNotTD(user.getPhone(), message);
                if(sendResult.startsWith("ERR")){
                    userNotifyRecordTMap.put("Status", 0);
                    userNotifyRecordTMap.put("ExceptionInfo", sendResult);
                }else{
                    userNotifyRecordTMap.put("Status", 1);
                }
                list.add(userNotifyRecordTMap);
                //消息推送
                Map<String, Object> notifyRecordParam = this.getNotifyRecordParam(message, workOrderId, 2, user);
                String clientId = redisClientUtils.hget(RedisKeys.geTuiKey(), user.getUserName());
                if (StringUtils.isEmpty(clientId)) {
                    // redis中不存在，查询数据库
                    RelationShipT relationShipT = relationShipTDao.getClientIdByUserNameString(user.getUserName());
                    if (null != relationShipT) {
                        clientId = relationShipT.getClientId();
                    }
                }
                if (StringUtils.isNotBlank(clientId)) {
                    boolean sendFlag = NotifyUtil.push2Single(clientId, title, message, 2, message);
                    if(sendFlag){
                        notifyRecordParam.put("Status", 1);
                    }else{
                        notifyRecordParam.put("Status", 0);
                        notifyRecordParam.put("ExceptionInfo", "pushNotify方法发送失败");
                    }
                }else {
                    notifyRecordParam.put("Status", 0);
                    notifyRecordParam.put("ExceptionInfo", "pushNotify----未查询到clientId");
                }
                list.add(notifyRecordParam);
            }
            //保存记录
            if (CollectionUtils.isNotEmpty(list)) {
                workOrderRepairDao.saveMsgOperation(list);
            }
        }
    }
    public Map<String, Object> getNotifyRecordParam( String message, Long workOrderId, int type,UserT user){
        Map<String, Object> userNotifyRecordTMap = new HashMap<>();
        String currentTime = LocalTimeUtil.getRealCurrentTime();
        userNotifyRecordTMap.put("OrderId", workOrderId);
        userNotifyRecordTMap.put("Function", 3); //服务商响应工单提醒
        userNotifyRecordTMap.put("Category", 1); //新工单提醒
        if (null != user) {
            userNotifyRecordTMap.put("ReceiverId", user.getId());
            userNotifyRecordTMap.put("ExceptionInfo", "");
        } else {
            userNotifyRecordTMap.put("ReceiverId", 0);
            userNotifyRecordTMap.put("ExceptionInfo", "未查询到该用户");
        }
        userNotifyRecordTMap.put("Content", message);
        userNotifyRecordTMap.put("SendType", type); //短信方式
        userNotifyRecordTMap.put("SendTime", currentTime);

        userNotifyRecordTMap.put("IsRead", 0);
        userNotifyRecordTMap.put("Flag", 0);
        userNotifyRecordTMap.put("Creator", user.getId());
        userNotifyRecordTMap.put("CreateTime", currentTime);

        return userNotifyRecordTMap;
    }
}
